﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using web.bussines;
using web.data.entities;
using web.core.Models;
using web.core.ViewModels;
using web.component;

namespace web.core.Controllers
{
    public class articleController : baseController
    {
        #region article_list
        public ActionResult article_list()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult ctr_article_list(int? cateId, int? page, int? pageSize)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);    //for multilanguage

            var alias_cate_str = "";
            var name_cate_str = "";

            //  int currentPage = 1;
            //int pageSize = 10;  //default pagesize = 10
            //if (string.IsNullOrEmpty(showStyle))
            //{
            //    showStyle = "grid";                 //default style is grid
            //}
            //ViewBag.style = showStyle;
            ////ViewBag.orderBy = "created"; //default order by product_name
            ////ViewBag.orderType = "desc"; //default sort by desc
            //return ListArticle(languageId, cateId, currentPage, pageSize, "created", "desc", showStyle);
            var orderBy = "created"; //default order by created date
            var orderType = "desc"; //default sort by desc
            page = Request.QueryString["page"] != null ? int.Parse(Request.QueryString["page"].ToString()) : 1;
            var total = 0;
            //List<SelectListItem> sizeDrop = GetSizePagingPublic(pageSize ?? 10);

            //List<SelectListItem> orderDrop = new List<SelectListItem>();
            //orderDrop.AddRange(new SelectListItem[] { 
            //                new SelectListItem{Selected=orderBy=="title"?true:false, Text = "Tiêu đề", Value = "title" },
            //                new SelectListItem{Selected=orderBy=="created"?true:false, Text = "Ngày đăng", Value = "created" },
            //                new SelectListItem{Selected=orderBy=="modifyed"?true:false, Text = "Cập nhật", Value = "modifyed" }
            //            });
            
            if (cateId != null)
            {
                var category = articleModels.GetViewArticleCategoryByID((int)cateId, languageId);
                if (category != null)
                {
                    alias_cate_str = string.IsNullOrEmpty(category.category_alias) != true ? category.category_alias : "tin_tuc";
                    name_cate_str = string.IsNullOrEmpty(category.category_name) != true ? category.category_name : "Tin tức";
                }
                else {
                    alias_cate_str = "tin_tuc";
                    name_cate_str = "Tin tức";
                }
                
            }
            else
            {
                alias_cate_str = "tin_tuc";
                name_cate_str = "Tin tức";
            }

            page = page ?? 1;
            var page_view = new ViewModels.PageListArticleViewModels() { 
                //list_pagesize = sizeDrop,
                pagesize = pageSize ?? 10,
                //list_order = orderDrop,
                 orderBy = orderBy,
                orderType = orderType,
                //showStyle = showStyle,
            };
            var lst_art =  Models.articleModels.GetListArticleByCategoryID(languageId, cateId, (page ?? 1), (pageSize ?? 10), orderBy, orderType, out total);
            page_view.page_list_article =lst_art;
            page_view.alias_cate = alias_cate_str;
            page_view.alias_name = name_cate_str;
            //page_view.list_category_article = Models.articleModels.g
           // page_view.art_category.
            page_view.total = total;
            
            return PartialView("../Shared/article/list", page_view);
        }

        /// <summary>
        /// Get metadata for article list
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult article_list_metadata(int? cateId)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var cate = articleModels.GetViewArticleCategoryByID(cateId ?? 0, languageId);
            var metadata_view = new ViewModels.MetadataViewModels();
            if (cate != null)
            {
                metadata_view.title = Ucommon.GetDefaultString (cate.category_title,  Ucommon.GetDefaultString (cate.category_name, "Tin tức"));
                metadata_view.keyword = Ucommon.GetDefaultString (cate.category_meta_keyword ,"");
                metadata_view.description = Ucommon.GetDefaultString (cate.category_meta_description, "");
            }
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }
        #endregion

        #region article detail
        //page article detail
        public ActionResult detail()
        {
            return View();
        }

        [ChildActionOnly]
        public ActionResult article_detail(int? id, string alias_cate)
        {
            var alias_cate_str = "";
            if (!string.IsNullOrEmpty(alias_cate))
            {
                alias_cate_str = alias_cate;
            }
            else {
                alias_cate_str = "tin_tuc";
            }
            //var numberImg = 5;
            var numberArticleRelate = 10;
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var article_view = new ViewModels.ArticleDetailViewModels();
            var v_article = articleModels.GetArticleViewByID(id ?? 0, languageId);
            if (v_article != null)
            {
                article_view.article_id = v_article.article_id;
                article_view.title = v_article.title;
                article_view.alias = v_article.alias;
                article_view.article_status = v_article.article_status;
                article_view.created = v_article.created;
                article_view.created_by = v_article.created_by;
                article_view.full_text = v_article.full_text;
                article_view.intro_text = v_article.intro_text;
                article_view.meta_key = v_article.meta_key;
                article_view.images = v_article.images;
                article_view.meta_desc = v_article.meta_desc;
                article_view.modifyed = v_article.modifyed;
                article_view.modifyed_by = v_article.modifyed_by;
                article_view.related = v_article.related;
                article_view.tag = v_article.tag;
                article_view.url = v_article.url;
                //article_view.image_display = v_article.image_display;
                //article_view.fileUpload = v_article.fileUpload;
                article_view.language_id = v_article.language_id;
                article_view.alias_cate = alias_cate_str; 

                // tin cung chuyen muc
                var lst_art_in_cate = articleModels.ListOtherInCategory(languageId, v_article.category_id, v_article.article_id, 6);
                article_view.list_article_in_cate = lst_art_in_cate;

                //article_view.list_images = productModels.GetListImagesByProduct(id ?? 0, numberImg);
                //article_view.list_article_relate = articleModels.GetListProductRelated(id ?? 0, numberArticleRelate);
            }

            return PartialView("../Shared/article/article_detail", article_view);
        }

        /// <summary>
        /// Get metadata for article detail
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult article_detail_metadata(int? id)
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var article = articleModels.GetArticleViewByID(id ?? 0, languageId);
            var metadata_view = new ViewModels.MetadataViewModels();
            if (article != null)
            {
                metadata_view.title = Ucommon.GetDefaultString(article.title, "");
                metadata_view.keyword = Ucommon.GetDefaultString(article.meta_key, "");
                metadata_view.description = Ucommon.GetDefaultString(article.meta_desc, "");
            }
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }
        #endregion

    }
}
